<template>
  <div class="father">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, idx) in list" :key="idx">
        <!-- 头像 -->
        <div class="left">
          <van-image round fit="cover" :src="item.aut_photo" />
        </div>
        <!-- 中间信息 -->
        <div class="mid">
          <div class="name">{{ item.aut_name }}</div>
          <div class="details">{{ item.content }}</div>
          <!-- 评论时间和回复条数 -->
          <div class="comments">
            <div class="time">{{ item.pubdate | relvTime }}</div>
            <van-button
              plain
              hairline
              type="default"
              size="mini"
              @click="replyCom(item)"
              >回复({{ item.reply_count }})</van-button
            >
          </div>
        </div>
        <!-- 右边图标 -->
        <div class="right">
          <van-button icon="like-o" type="info" />
        </div>
      </van-cell>
      <!-- 发表区域 -->
      <div class="send-box">
        <van-field
          v-model="sms"
          @keyup.enter="send"
          center
          clearable
          placeholder="发表你的观点"
        >
          <template #button>
            <van-button size="small" @click="send" type="info">发布</van-button>
          </template>
        </van-field>
      </div>
    </van-list>
    <van-action-sheet v-model="replyShow" title="标题">
      <replyInfo :cmt="cmt" v-if="replyShow" />
    </van-action-sheet>
  </div>
</template>

<script>
import { getCommentsAPI, sendCommentsAPI } from '@/api'
import replyInfo from './reply.vue'
export default {
  name: 'myComment',
  components: {
    replyInfo
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      lastID: null,
      endID: '',
      sms: '',
      replyShow: false,
      cmt: {}
    }
  },
  methods: {
    replyCom (item) {
      this.cmt = item
      this.replyShow = true
    },
    async onLoad () {
      const res = await getCommentsAPI({
        type: 'a',
        source: this.$route.params.id,
        offset: this.lastID,
        limit: 4
      })
      this.list.push(...res.data.data.results)
      this.lastID = res.data.data.last_id
      if (res.data.data.end_id === this.lastID) {
        this.finished = true
      }
      this.loading = false
    },
    // 发表评论
    async send () {
      if (!this.sms) return this.$toast('请输入评论内容')
      const res = await sendCommentsAPI({
        target: this.$route.params.id,
        content: this.sms
      })
      this.list.unshift(res.data.data.new_obj)
      this.sms = ''
      this.$toast.success('评论成功')
      console.log('发表评论', res)
    }
  }
}
</script>

<style lang="less" scoped>
.father {
  padding-bottom: 100px;
  .van-cell {
    .mid {
      flex: 1;
      margin-left: 10px;
      .comments {
        display: flex;
        .van-button {
          width: 50px;
        }
      }
    }
    ::v-deep .van-cell__value {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        .van-image {
          width: 50px;
          height: 50px;
        }
      }
    }
  }
}
.send-box {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
  ::v-deep .van-field__body {
    width: 100%;
  }
}
</style>
